package Juc;

import java.util.concurrent.Semaphore;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:34398
 * Date:2024-08-03
 * Time:15:28
 */
public class SemaphoreTest {

    public static void main(String[] args) {
        Semaphore semaphore=new Semaphore(4);

        for (int i = 0; i <20 ; i++) {
            int id=i;
            Thread t=new Thread(()->{
                try {
                    System.out.println("申请资源");
                    semaphore.acquire();//申请资源的方法
                    System.out.println("执行任务："+id);
                    System.out.println("释放资源");
                    semaphore.release();//释放资源的方法

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
        }
    }
}
